/**
 *
 */
package com.zhuoyue.zhtj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.zhtj.entity.Znxx;
import com.zhuoyue.zhtj.service.ZnxxService;

/**
 * 职能Controller
 * @author hss
 * @version 2017-08-15
 */
@Controller
@RequestMapping(value = "${adminPath}/zhtj/znxx")
public class ZnxxController extends BaseController {

	@Autowired
	private ZnxxService znxxService;
	
	@ModelAttribute
	public Znxx get(@RequestParam(required=false) String id) {
		Znxx entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = znxxService.get(id);
		}
		if (entity == null){
			entity = new Znxx();
		}
		return entity;
	}
	
	@RequiresPermissions("zhtj:znxx:view")
	@RequestMapping(value = {"list", ""})
	public String list(Znxx znxx, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Znxx> page = znxxService.findPage(new Page<Znxx>(request, response), znxx); 
		model.addAttribute("page", page);
		return "zhuoyue/zhtj/znxxList";
	}

	@RequiresPermissions("zhtj:znxx:view")
	@RequestMapping(value = "form")
	public String form(Znxx znxx, Model model) {
		model.addAttribute("znxx", znxx);
		return "zhuoyue/zhtj/znxxForm";
	}

	@RequiresPermissions("zhtj:znxx:edit")
	@RequestMapping(value = "save")
	public String save(Znxx znxx, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, znxx)){
			return form(znxx, model);
		}
		znxxService.save(znxx);
		addMessage(redirectAttributes, "保存职能成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/znxx/?repage";
	}
	
	@RequiresPermissions("zhtj:znxx:edit")
	@RequestMapping(value = "delete")
	public String delete(Znxx znxx, RedirectAttributes redirectAttributes) {
		znxxService.delete(znxx);
		addMessage(redirectAttributes, "删除职能成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/znxx/?repage";
	}

}